package com.yiyuan.app.service.impl;

import com.guaike.core.entity.result.Result;
import com.guaike.core.redis.context.ApiContext;
import com.guaike.core.utils.ListUtil;
import com.yiyuan.app.service.BaseUtilService;
import com.yiyuan.app.service.HomeService;
import com.yiyuan.app.dao.*;
import com.yiyuan.app.entity.enums.*;
import com.yiyuan.app.entity.po.*;
import com.yiyuan.app.entity.result.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
@AllArgsConstructor
public class HomeServiceImpl implements HomeService {

    private final UAppUserDAO uAppUserDAO;
    private final UAppUserEquityDAO uAppUserEquityDAO;
    private final MRecordNoticeDAO mRecordNoticeDAO;
    private final OBannerDAO oBannerDAO;
    private final MProductPackageDAO mProductPackageDAO;
    private final MConfPrizeDAO mConfPrizeDAO;
    private final BaseUtilService baseUtilService;
    private final DOrderRechargeDAO orderRechargeDAO;
    private final SysConfDAO sysConfDAO;


    @Override
    public Result<HomeUserInfoVO> userInfo() {
        AppUser currentUser = ApiContext.getCurrentUser();
        UAppUserPO uAppUserPO = uAppUserDAO.findById(currentUser.getUserId());
        UAppUserEquityPO userEquityPO = uAppUserEquityDAO.findOne(UAppUserEquityPO::getUserId, currentUser.getUserId());
        HomeUserInfoVO vo = new HomeUserInfoVO();
        BeanUtils.copyProperties(uAppUserPO, vo);
        BeanUtils.copyProperties(userEquityPO, vo);
        MProductPackagePO packagePO = mProductPackageDAO.findActivityPackage();
        if (Objects.nonNull(packagePO)) {
            vo.setActivityName(packagePO.getProductName());
        }
        if (baseUtilService.checkNewSign()) {
            MConfPrizePO prizePO = mConfPrizeDAO.findOne(MConfPrizePO::getAction, PrizeActionEnum.new_sign.getCode());
            vo.setSignName(prizePO.getTaskName());
        } else if (baseUtilService.checkSign()) {
            MConfPrizePO prizePO = mConfPrizeDAO.findOne(MConfPrizePO::getAction, PrizeActionEnum.sign.getCode());
            vo.setSignName(prizePO.getTaskName());
        }
        if (!uAppUserPO.getIsCertify()) {
            vo.setIsCertify(Boolean.FALSE);
        }
        return Result.success().data(vo).build();
    }

    @Override
    public Result<List<NoticeVO>> noticeList() {
        List<MRecordNoticePO> pos = mRecordNoticeDAO.findNoticeList();
        List<NoticeVO> vos = new ArrayList<>();
        pos.forEach(po -> {
            UAppUserPO uAppUserPO = uAppUserDAO.findById(po.getUserId());
            String prizeType;
            if (po.getNoticeType().equals(NoticeTypeEnum.NUMBER.getCode())) {
                prizeType = po.getScore().toString();
            } else {
                prizeType = NoticeTypeEnum.getByCode(po.getNoticeType()).getDesc();
            }
            NoticeVO vo = new NoticeVO();
            vo.setNickName(uAppUserPO.getNickname());
            vo.setPrizeType(prizeType);
            vo.setPinecone(po.getPinecone());
            vo.setRoomName(po.getRoomName());
            vos.add(vo);
        });
        return Result.success().data(vos).build();
    }

    @Override
    public Result<BannerVO> bannerInfo() {
        List<Integer> list = Arrays.asList(BannerTypeEnum.banner_1.getCode(), BannerTypeEnum.banner_2.getCode(), BannerTypeEnum.banner_3.getCode());
        List<OBannerPO> pos = oBannerDAO.findIn(OBannerPO::getBannerType, list, OBannerPO::getId);
        Map<Integer, List<OBannerPO>> listMap = pos.stream().collect(Collectors.groupingBy(OBannerPO::getBannerType));
        BannerVO vo = new BannerVO();
        for (Integer bannerType : listMap.keySet()) {
            List<OBannerPO> bannerPOS = listMap.get(bannerType);
            bannerPOS = bannerPOS.stream().sorted(Comparator.comparing(OBannerPO::getId).reversed()).collect(Collectors.toList());
            List<ImageVO> vos = ListUtil.copyList(bannerPOS, ImageVO.class);
            if (bannerType.equals(BannerTypeEnum.banner_1.getCode())) {
                vo.setBannerOneList(vos);
            } else if (bannerType.equals(BannerTypeEnum.banner_2.getCode())) {
                vo.setBannerTwoList(vos);
            } else if (bannerType.equals(BannerTypeEnum.banner_3.getCode())) {
                vo.setBannerThreeList(vos);
            }
        }
        return Result.success().data(vo).build();
    }

    @Override
    public Result<NoviceGiftPackVO> getNewUserPackage() {
        NoviceGiftPackVO vo = new NoviceGiftPackVO();
        AppUser currentUser = ApiContext.getCurrentUser();
        DOrderRechargePO rechargePO = orderRechargeDAO.findNoviceGiftPack(currentUser.getUserId(), OrderStatusEnum.pay_success.getCode());
        MProductPackagePO packagePO = mProductPackageDAO.findNewPersonPackage();
        vo.setIsPop(Objects.nonNull(packagePO) && Objects.isNull(rechargePO));
        if (vo.getIsPop()) {
            PackageVO packageVO = new PackageVO();
            BeanUtils.copyProperties(packagePO, packageVO);
            vo.setPackageVO(packageVO);
        }
        return Result.success().data(vo).build();
    }

    @Override
    public Result<GiftPackVO> getGiftPackage() {
        AppUser currentUser = ApiContext.getCurrentUser();
        GiftPackVO vo = new GiftPackVO();
        DOrderRechargePO rechargePO = orderRechargeDAO.findNoviceGiftPack(currentUser.getUserId(), OrderStatusEnum.pay_success.getCode());
        MProductPackagePO newPackagePO = mProductPackageDAO.findNewPersonPackage();
        if (Objects.nonNull(newPackagePO) && Objects.isNull(rechargePO)) {
            PackageVO newPackageVO = new PackageVO();
            BeanUtils.copyProperties(newPackagePO, newPackageVO);
            vo.setNewPackageVO(newPackageVO);
        }
        MProductPackagePO packagePO = mProductPackageDAO.findActivityPackage();
        if (Objects.nonNull(packagePO)) {
            PackageVO packageVO = new PackageVO();
            BeanUtils.copyProperties(packagePO, packageVO);
            vo.setPackageVO(packageVO);
        }
        return Result.success().data(vo).build();
    }

    @Override
    public Result<Boolean> checkRealNameButton() {
        SysConfPO confPO = sysConfDAO.findOne(ConfCodeEnum.real_name_button.getCodeParent(), ConfCodeEnum.real_name_button.getCodeSecond());
        if (Objects.isNull(confPO)) {
            return Result.success().data(Boolean.TRUE).build();
        }
        if (confPO.getConfValue().equalsIgnoreCase("on")) {
            return Result.success().data(Boolean.TRUE).build();
        }
        return Result.success().data(Boolean.FALSE).build();
    }
}
